@isTest
private class EmailVelocityCheckTester
{
  public static Integer NUM = 2;
  public static Integer THRESHFLOOR = 1;
  public static Boolean createEmailMessage(String fromS, String toS, ID id)
  {
    //Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    EmailMessage mail = new EmailMessage();
    //String[] toAddresses = new String[] {to.Email};
    mail.fromAddress = fromS;
    mail.toAddress = toS;
    //mail.setToAddresses(toAddresses);
    mail.Subject = 'Email Velocity Unit Test';
    mail.TextBody = 'velocity unit test';
    mail.ParentId = id;
    mail.Incoming = false;
    mail.MessageDate =  System.now();
    Database.Saveresult sr = Database.insert(mail);
    return sr.isSuccess();
  }
  
  
  public static testMethod void testEmailVelocity() 
  {
    List<User> users  = new List<User>();
    for(Integer i = 0; i < NUM; i++)
    {
      User u_new = new User();
      u_new.UserName = 'xyz_1234567890@abcd.com' + i;
      u_new.LastName = 'Michael Shu Test Email' + i;
      u_new.Email = 'sshu@attinteractive.com';
      u_new.Alias = 'abcde' + i;
      u_new.CommunityNickname = 'xyaz_1234556' + i;
      u_new.TimeZoneSidKey = 'America/Los_Angeles';
      u_new.LocaleSidKey = 'en_US';
      u_new.EmailEncodingKey = 'UTF-8';
      u_new.ProfileId = [select id from Profile where Name like '%IOG Support%' limit 1].Id;
      u_new.UserRoleId = [select id from UserRole limit 1].Id ;
      u_new.LanguageLocaleKey = 'en_US';
      u_new.IsActive = true;
      insert u_new;
    }
    users = [select FirstName from User where UserName like 'xyz_1234567890@abcd.com%'];
    System.assertEquals(NUM, users.size());
    
    User u = new User();
    u.UserName = 'xyz_1234567890@atti-temp.com' + 'x';
    u.LastName = 'Michael Shu Test Email' + 'x';
    u.Email = 'xyz_1234567890@atti.com';
    u.Alias = 'abcde' + 'x';
    u.CommunityNickname = 'xyaz_1234556' + 'x';
    u.TimeZoneSidKey = 'America/Los_Angeles';
    u.LocaleSidKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    u.ProfileId = [select ID from Profile where Name = 'System Administrator' limit 1].Id;
    u.UserRoleId = [select ID from UserRole where Name = 'Information Systems' limit 1].Id;
    u.LanguageLocaleKey = 'en_US';
    Database.Saveresult sr = Database.insert(u);
    
    User sysUser = [select ID from User where ID =: sr.getID() limit 1];
    
    String csSubject = 'testSubject_email';
    String csDescription = 'This is for email test only';
    Case newCase = new Case(subject = csSubject
                          , description = csDescription
                          , IR_Priority__c = 'Standard'
                          , recordTypeId = RecordTypeHelper.GetRecordTypeID('Case',  'IOG Incident'));
    System.runAs(users[0])
    {
      insert newCase;
    }
    Case curCase = [select Id from Case where OwnerId =: users[0].Id limit 1];
    Boolean res;   
    System.runAs(sysUser)
    {    
        
        // create 3 EmailMessage objects in quick succession
        res = createEmailMessage('xyz_1234567890@attinteractive.com', 'xyz_1234567890@attinteractive.com', curCase.Id);
        System.assertEquals(true, res);
        res = createEmailMessage('xyz_1234567890@attinteractive.com', 'xyz_1234567890@attinteractive.com', curCase.Id);
        System.assertEquals(true, res);
        res = createEmailMessage('xyz_1234567890@attinteractive.com', 'xyz_1234567890@attinteractive.com', curCase.Id);
        System.assertEquals(true, res);
    }
    
    EmailVelocityHandler ev = new EmailVelocityHandler();
    List<EmailVelocityHandler.Hist> items = ev.handle(1, 3);
    System.Debug(items.size());
    res = (items.size() >= THRESHFLOOR);
    System.assertEquals(true, res);
    
    
  }
}